package com.ruoyi.controller;


import com.ruoyi.api.common.R;
import com.ruoyi.domain.SpaceDirectory;
import com.ruoyi.dto.SpaceDirectoryTree;
import com.ruoyi.platform.annotation.Log;
import com.ruoyi.platform.constant.UserConstants;
import com.ruoyi.platform.core.controller.BaseController;
import com.ruoyi.platform.core.domain.AjaxResult;
import com.ruoyi.platform.core.page.TableDataInfo;
import com.ruoyi.platform.enums.BusinessType;
import com.ruoyi.platform.utils.StringUtils;
import com.ruoyi.platform.utils.poi.ExcelUtil;
import com.ruoyi.service.ISpaceDirectoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智联空间文件夹Controller
 *
 * @author ruoyi
 * @date 2022-12-19
 */
@RestController
@RequestMapping("/admin/directory")
@Api(tags = {"智联空间文件夹服务"})
public class SpaceDirectoryController extends BaseController {

    @Autowired
    private ISpaceDirectoryService spaceDirectoryService;

    /**
     * 查询智联空间文件夹列表
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询智联空间文件夹列表", httpMethod = "GET", notes = "查询智联空间文件夹列表")
    public TableDataInfo list(SpaceDirectory spaceDirectory) {
        startPage();
        List<SpaceDirectory> list = spaceDirectoryService.selectSpaceDirectoryList(spaceDirectory);
        return getDataTable(list);
    }

    /**
     * 查询文档列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:query')")
    @GetMapping("/exclude")
    public AjaxResult excludeChild(@RequestParam(value = "id", required = false) Long id) {
        List<SpaceDirectory> list = spaceDirectoryService.selectSpaceDirectoryList(new SpaceDirectory());
        list.removeIf(d -> d.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
        return AjaxResult.success(list);
    }

    /**
     * 导出智联空间文件夹列表
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:export')")
    @Log(title = "智联空间文件夹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出智联空间文件夹列表", httpMethod = "POST", notes = "导出智联空间文件夹列表")
    public void export(HttpServletResponse response, SpaceDirectory spaceDirectory) {
        List<SpaceDirectory> list = spaceDirectoryService.selectSpaceDirectoryList(spaceDirectory);
        ExcelUtil<SpaceDirectory> util = new ExcelUtil<>(SpaceDirectory.class);
        util.exportExcel(response, list, "智联空间文件夹数据");
    }

    /**
     * 获取智联空间文件夹详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取智联空间文件夹详细信息", httpMethod = "GET", notes = "获取智联空间文件夹详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(spaceDirectoryService.selectSpaceDirectoryById(id));
    }

    /**
     * 新增智联空间文件夹
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:add')")
    @Log(title = "智联空间文件夹", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增智联空间文件夹", httpMethod = "POST", notes = "新增智联空间文件夹")
    public AjaxResult add(@RequestBody SpaceDirectory spaceDirectory) {
        if (UserConstants.NOT_UNIQUE.equals(spaceDirectoryService.checkDirectoryNameUnique(spaceDirectory))) {
            return AjaxResult.error("新增文件夹'" + spaceDirectory.getName() + "'失败，文件夹已存在");
        }
        return toAjax(spaceDirectoryService.insertSpaceDirectory(spaceDirectory));
    }

    /**
     * 修改智联空间文件夹
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:edit')")
    @Log(title = "智联空间文件夹", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改智联空间文件夹", httpMethod = "PUT", notes = "修改智联空间文件夹")
    public AjaxResult edit(@RequestBody SpaceDirectory spaceDirectory) {
        return toAjax(spaceDirectoryService.updateSpaceDirectory(spaceDirectory));
    }

    /**
     * 删除文件夹
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:remove')")
    @Log(title = "删除文件夹", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(spaceDirectoryService.deleteById(id));
    }

    /**
     * 删除智联空间文件夹
     */
    @PreAuthorize("@ss.hasPermi('admin:directory:remove')")
    @Log(title = "智联空间文件夹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除智联空间文件夹", httpMethod = "DELETE", notes = "删除智联空间文件夹")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(spaceDirectoryService.deleteSpaceDirectoryByIds(ids));
    }

    /**
     * 文件夹目录树
     */
    @GetMapping("/treeData")
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:query')")
    @ApiOperation(value = "文件夹目录树", httpMethod = "GET", notes = "文件夹目录树")
    @Log(title = "智联空间文件夹", businessType = BusinessType.QUERY)
    public R treeData() {
        List<SpaceDirectoryTree> treeList = spaceDirectoryService.getTreeList(new SpaceDirectory());
        return R.ok(treeList);
    }


}
